<script setup>
import Tabbarstudent from "../../pages/componentstudent/Tabbarstudent.vue";
import { ref, computed, onMounted } from 'vue'

// 数据定义
const remainingDays = ref(4)
const reason = ref('')
const agreed = ref(false)
const uploadedImage = ref('')

// 模拟的经纬度坐标 (山西省太原市尖草坪区中北大学)
// 注意：这些是近似坐标，实际应用中应使用更精确的坐标
const currentLocation = ref('');
const mockLocations = [
  '山西省大同市云岗区朝阳小区2单元3号楼',
];
let currentMockIndex = 0;
const simulateLocation = () => {
  currentLocation.value = mockLocations[currentMockIndex];
  currentMockIndex = (currentMockIndex + 1) % mockLocations.length; // 循环切换模拟位置
};
// 计算属性：判断是否可以提交
const canSubmit = computed(() => {
  return reason.value.trim().length > 0 && agreed.value && uploadedImage.value
})

// 方法定义
const startFilling = () => {
  // 跳转到健康申报书页面
  uni.navigateTo({
    url: 'shengbaoshu'
  });
}

const downloadAttachment = () => {
  uni.showToast({
    title: '附件已下载',
    icon: 'none'
  })
}

const chooseImage = () => {
  uni.chooseImage({
    count: 1,
    sizeType: ['original', 'compressed'],
    sourceType: ['album', 'camera'],
    success: (res) => {
      uploadedImage.value = res.tempFilePaths[0]
    }
  })
}

const toggleAgreement = () => {
  agreed.value = !agreed.value
}

const submitApplication = () => {
  if (!canSubmit.value) {
    uni.showToast({
      title: '请完善所有信息',
      icon: 'none'
    })
    return
  }

  uni.showToast({
    title: '申请已提交',
    icon: 'success'
  })
  uni.navigateTo({
    url: 'success_fanxiao',
  });
}
</script>

<template>
  <view>
    <view id="fanxiao">
      <view class="page">
        <view class="container">
          <!-- 返校说明 -->
          <view class="section">
            <text class="title">返校说明</text>
            <text class="description">因受疫情影响，学生需按学校计划批次返校，未接收到返校通知的学生，请耐心等待。</text>
            <text class="description">本次返校需经学校审核同意后，才能返校。不得未经学校批准返校。</text>
          </view>

          <!-- 返校时间等信息 -->
          <view class="section">
            <text class="subtitle">返校时间：2025年10月8日至2025年10月12日</text>
            <view class="info-box">
              <text>申请开始时间：2025年10月08日 00:00:00</text>
              <text>申请结束时间：2025年10月12日 00:00:00</text>
              <text>剩余时间：{{ remainingDays }}天</text>
            </view>
          </view>

          <!-- 当前定位 -->
          <view class="section">
            <text class="subtitle">当前定位</text>
            <view class="location-box">
              <view
                  @click="simulateLocation"
                  style="border: 1px solid #dddddd; border-radius: 4px; padding: 12px; font-size: 14px;height: 10px; width: 200px; cursor: pointer; color: #000000;"
              >
                {{ currentLocation || '点击定位' }}
              </view>
          </view>
          </view>

          <!-- 健康情况申报卡 -->
          <view class="section">
            <text class="subtitle">健康情况申报卡</text>
            <view class="health-card">
              <button @click="startFilling ">开始填写</button>
            </view>
          </view>

          <!-- 申请理由 -->
          <view class="section">
            <text class="subtitle">申请理由：</text>
            <textarea v-model="reason" placeholder="请输入" maxlength="500"></textarea>
            <text class="count">{{ reason.length }}/500</text>
          </view>

          <!-- 返校承诺书上传 -->
          <view class="section">
            <text class="subtitle">请上传返校承诺书</text>
            <text class="download-link" @click="downloadAttachment">点击下载附件</text>
            <view class="upload-box" @click="chooseImage">
              <image src="/static/upload-icon.png" mode="aspectFit"></image>
              <text>上传</text>
            </view>
            <view v-if="uploadedImage" class="uploaded-image">
              <image :src="uploadedImage" mode="aspectFit"></image>
            </view>
            <text>务必上传签字图片</text>
            <text>支持扩展名：JPEG、PNG、JPGE限制大小1M以下、1张</text>
          </view>

          <!-- 承诺勾选 -->
          <view class="section">
            <label class="checkbox-label">
              <checkbox :checked="agreed" @click="toggleAgreement" />
              <text>我承诺以上信息无谎报、瞒报</text>
            </label>
          </view>

          <!-- 提交按钮 -->
          <view class="submit-button">
            <button :disabled="!canSubmit" @click="submitApplication">提交申请</button>
          </view>
        </view>
  </view>
    <Tabbarstudent :cur-index="2"></Tabbarstudent>
    </view>
  </view>
</template>

<style scoped>
#fanxiao{
  width: 100%;
  height: calc(100% - 50px);
  background-color:#F0F3FA;
}
.page {
  height: 100vh;
  background-color: #F0F3FA;
}

.container {
    width: 100%;
    padding: 20px;
    box-sizing: border-box;
    background-color:#F0F3FA;
  }

.section {
  background-color:#F0F3FA ;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin-bottom: 12px;
}

.description {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
  display: block;
  margin: 8px 0;
}

.subtitle {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 12px;
  display: block;
}

.info-box {
  background-color: #F0F3FA;
  padding: 12px;
  border-radius: 6px;
  font-size: 14px;
  color: #666;
}

.info-box text {
  display: block;
  margin: 4px 0;
}

.location-box, .health-card, .upload-box {
  display: flex;
  align-items: center;
  margin-top: 8px;
}

.location-box image, .health-card image, .upload-box image {
  width: 20px;
  height: 20px;
  margin-right: 8px;
}

.health-card button, .upload-box text {
  font-size: 14px;
  color: #007AFF;
}

.uploaded-image {
  margin-top: 12px;
  text-align: center;
}

.uploaded-image image {
  width: 120px;
  height: 120px;
  border-radius: 8px;
  border: 1px solid #eee;
}

textarea {
  width: 100%;
  height: 100px;
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 12px;
  font-size: 14px;
  margin: 8px 0;
  box-sizing: border-box;
}

.count {
  font-size: 12px;
  color: #999;
  text-align: right;
  margin-top: 4px;
}

.download-link {
  color: #007AFF;
  font-size: 14px;
  margin: 8px 0;
  display: inline-block;
}

.checkbox-label {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #333;
}

.checkbox-label checkbox {
  margin-right: 8px;
}

.submit-button {
  margin: 24px 0;
}

.submit-button button {
  width: 100%;
  height: 48px;
  background-color: #007AFF;
  color: white;
  font-size: 16px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 122, 255, 0.3);
}

.submit-button button:disabled {
  background-color: #cccccc;
  color: #999;
}

</style>